package org.zhxy.zhxy.utils;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class EmailUtils {


    public static final String MAIL_SENDER="chpincp@qq.com";
    @Resource
    private JavaMailSender javaMailSender;//注入QQ发送邮件的bean
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public  void sendEmail(String email,String code){
        try {
            SimpleMailMessage mailMessage= new SimpleMailMessage();
            mailMessage.setFrom(MAIL_SENDER);//发送者
            mailMessage.setTo(email);//接收者
            mailMessage.setSubject("智慧校园邮箱注册/换绑");//邮件标题
            mailMessage.setText(code+",验证码有效期为5分钟");//邮件内容
            javaMailSender.send(mailMessage);
            //存入redis
            stringRedisTemplate.opsForValue().set("email"+":"+email,code,5, TimeUnit.MINUTES);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
